package com.wxj.bank.module.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * @author shengtao
 * @Description: 日期工具类
 * @date 2019/01/30 10:50
 */
public class DateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);
    /**
     * 默认日期格式
     */
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 日期格式 yyyyMMdd
     */
    public static final String DATE_PATTERN_YYYYMMDD = "yyyyMMdd";

    /**
     * 日期格式 yyyyMMddHHmm
     */
    public static final String DATE_PATTERN_YYYYMMDDHHMM = "yyyyMMddHHmm";

    /**
     * 默认时间戳格式，到毫秒 yyyyMMddHHmmssSSS
     */
    public static final String DATEDETAIL_PATTERN_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    /**
     * 默认时间戳格式，到秒 yyyyMMddHHmmss
     */
    public static final String DATEDETAIL_PATTERN_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    /**
     * 默认时间格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 统计时间格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String TONGJI_DATETIME_PATTERN = "yyyy-MM-dd HH:mm";
    /**
     * 默认时间戳格式，到毫秒 yyyy-MM-dd HH:mm:ss SSS
     */
    public static final String DEFAULT_DATEDETAIL_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";

    /**
     * 日期转字符串
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date,String format){
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 秒转字符串
     * @param microseconds
     * @param format
     * @return
     */
    public static String dateToString(long microseconds,String format){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(microseconds);
        return dateToString(c.getTime(),format);
    }

    /**
     * 带时区的日期转换字符串
     * @param date
     * @param format
     * @param locale
     * @return
     */
    public static String dateToStringLocal(Date date, String format, Locale locale){
        return new SimpleDateFormat(format,locale).format(date);
    }

    /**
     * 时间差值-分钟
     * @param before
     * @param after
     * @return
     */
    public static long differenceBySecond(Date before,Date after){
        if(before == null || after == null){
            return 0L;
        }
        return (after.getTime() - before.getTime())/1000;
    }

    /**
     * 单位时间转日期
     * @param time
     * @param unit
     * @return
     */
    public static Date longToDate(long time,TimeUnit unit){
        Calendar c = Calendar.getInstance();
        switch (unit){
            case DAYS:
                time = time * 24 * 60 * 60 * 1000;
                break;
            case HOURS:
                time = time * 60 * 60 * 1000;
                break;
            case MINUTES:
                time = time * 60 * 1000;
                break;
            case SECONDS:
                time = time * 1000;
                break;
            default:
                break;
        }
        c.setTimeInMillis(time);
        return c.getTime();
    }

    /**
     * 时间字符串转日期
     * @param time
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String time,String format) throws ParseException {
        return new SimpleDateFormat(format).parse(time);
    }

    /**
     * 时间带时区字符串转日期
     * @param time
     * @param format
     * @param locale
     * @return
     * @throws ParseException
     */
    public static Date stringToDateWithLocale(String time,String format,Locale locale) throws ParseException {
        return new SimpleDateFormat(format,locale).parse(time);
    }

    /**
     * 获取当前时间-YYYY-MM-DD
     * @return
     */
    public static Date getToday(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        return c.getTime();
    }

    /**
     * 获取日期单位值
     * @param date
     * @param filed
     * @return
     */
    public static int getSegmentOfDate(Date date,int filed){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(filed);
    }

    /**
     * 获取日期差值
     * @param before
     * @param after
     * @return
     */
    public static long differenceByMinute(Date before,Date after) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(before);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(after);
        return (calendar2.getTimeInMillis() - calendar1.getTimeInMillis())/(1000*60);
    }
}
